package com.dss.dsc.serlvet;

import javax.servlet.http.HttpServletRequest;

import org.eclipse.jetty.websocket.WebSocket;
import org.eclipse.jetty.websocket.WebSocketServlet;
import org.json.JSONException;
import org.json.JSONObject;

import com.dss.dsc.model.ContentsModel;
import com.dss.dsc.server.WebAppServer;
import com.dss.wap.core.webserver.GefConvert;

import dsc.model.util.JsonConvert;
import dsc.model.view.SysForView;

public class SysForViewSaveServlet extends WebSocketServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public SysForViewSaveServlet() {
		// TODO Auto-generated constructor stub
	}

	public WebSocket doWebSocketConnect(HttpServletRequest arg0, String arg1) {
		// TODO Auto-generated method stub
		return new TailorSocket();
	}
	
	class TailorSocket implements WebSocket.OnTextMessage {
		private Connection connection;
		public void onClose(int arg0, String arg1) {
			// TODO Auto-generated method stub
			
		}

		public void onOpen(Connection arg0) {
			// TODO Auto-generated method stub
			this.connection = arg0;
			this.connection.setMaxTextMessageSize(5000000);
		}

		public void onMessage(String arg0) {
			// TODO Auto-generated method stub
			System.out.println(arg0);
			JsonConvert jConvert = JsonConvert.instance();
			GefConvert gConvert = GefConvert.getInstance();
			SysForView sysForView = new SysForView();
			jConvert.json2SysForView(arg0, sysForView);
			
			System.out.println("test - --------------------------------------");
			System.out.println(jConvert.sysForView2Json(sysForView));
//			ContentsModel contents = gConvert.convertSysForView(sysForView);
			JSONObject json;
			try {
				json = new JSONObject(arg0);
				String path = WebAppServer.readIni("rsc.ini") + "eclipse/" + json.getString("presenterName")+".dsc";//"C:\\Documents and Settings\\Administrator\\桌面\\webEditor\\eclipse\\" + json.getString("presenterName")+".dsc";
				gConvert.writeFile(sysForView, path);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}			
			this.connection.close();
		}
		
	}

}
